package com.zyh.day03

object ImplicitTest {
  //隐式转换函数:形参必须是一个
  //Implicit conversion function: formal parameter must be one only
  implicit def string2Int(str:String): Int = Integer.parseInt(str)

  def main(args: Array[String]): Unit = {
    //隐式转换:隐形自动的转换
    //val i:Int = "10".toInt//显示的转换

    //当编译器发现一个代码,需要进行类型转换 而是先从当前作用范围和原始类型和目标类型的伴生对象中寻找有无可用的隐式转换
    val i:Int = "10"//隐式的转换
    val j:Int = string2Int("10")//显示的转换

    hello("baizhi")
    hello
    val s1 = Student("xiaohei",18,100)
    val s2 = Student("xiaobai",20,80)
    maxStudent(s1,s2)(_.age - _.age)
  }

  implicit val msg:String = "world"
  //implicit修饰的参数,叫做隐式参数,隐式参数在调用的时候可以不用赋值,编译器会自动从当前的作用范围内寻找使用implicit修饰的相同类型的值 自动赋值
  def hello(implicit str:String): Unit = {
    println(s"str = ${str}")
  }

  //为函数参数隐式赋值 需结合柯里化 因为implicit关键字适用于单参数列表
  implicit def maxStudentByAge(s1:Student,s2:Student): Int = s1.age - s2.age
  def maxStudent(s1:Student,s2:Student)(implicit compare:(Student,Student)=>Int): Student = {
    if(compare(s1,s2) > 0) s1 else s2
  }
}
